<?php

class Application_Model_dataDAO_DanhMucDAO implements Application_Model_dataDAOInf_DanhMucDAOInf {
	protected static $_instance=null;
	
	protected function __construct(){
		
	}
	public static function getInstance(){
		if(self::$_instance==null) {
			self::$_instance = new self();
		}
		return self::$_instance;
	}
	
	/**
	 * @param $id
	 * @param $tenDanhMuc
	 * @param $chuThich
	 */
	public function insert($ten, $chuThich) {
		$result=0;
		try {
			$db = Application_Model_dataDAO_DBConnect::conn();
			$dmarray = array ("tenDanhMuc" => $ten, "chuThich" => $chuThich );
			$db->insert ( 'tb_danhmucsanpham', $dmarray );
			$result=$db->lastInsertId ();
			$db->closeConnection ();
		} catch ( Zend_Db_Exception $e ) {
			echo $e->getMessage ();
		}
		return $result;
	}
	/**
	 * tra ve danh sach cac danh muc
	 */
	public function load() {
		$result=array();
		try {
			$db=Application_Model_dataDAO_DBConnect::conn();
			$statement="SELECT * FROM tb_danhmucsanpham";
			$res=$db->fetchAll($statement);
			
			$i=0;
			foreach ($res as $value) {
				$danhMuc=new Application_Model_bean_DanhMucSanPham($value['maDanhMucSanPham'],$value['tenDanhMuc'],$value['chuThich']);
				$result[$i]=$danhMuc;
				$i++;
			}
			$db->closeConnection();
		} catch (Zend_Db_Exception  $e) {
			echo $e->getMessage();
		}
		return $result;
	}

	public function delete($id) {
		$result1=false;
		try {
			$db=Application_Model_dataDAO_DBConnect::conn();
			$conditions[] = 'maDanhMucSanPham='.$id;
			$result=$db->delete('tb_danhmucsanpham',$conditions);
			if($result==1)
			{
				$result1=true;
			}
			$db->closeConnection();
		} catch (Zend_Db_Exception  $e) {
			echo $e->getMessage();
		}
		
		return $result1;		
	}

	public function loadOne($id) {
		$result=null;
		try {
			$stament="SELECT maDanhMucSanPham,tenDanhMuc,chuThich FROM tb_danhmucsanpham WHERE maDanhMucSanPham=".$id;
			$db=Application_Model_dataDAO_DBConnect::conn();
			$result=$db->fetchAll($stament);
			foreach ($result as $value) {
				$result=new Application_Model_bean_DanhMucSanPham($value['maDanhMucSanPham'],$value['tenDanhMuc'],$value['chuThich']);
			}
			$db->closeConnection();
		} catch (Zend_Db_Exception  $e) {
			echo $e->getMessage();
		}
		
		return $result;
	}
	/**
	 * @param int $id
	 * @param string $tenMoi
	 * @param string $chuThichMoi
	 */
	public function update($id, $tenMoi, $chuThichMoi) {
		$result=false;
		try {
			$update=array("tenDanhMuc" => $tenMoi,"chuThich" => $chuThichMoi);
			$conditions[] = 'maDanhMucSanPham='.$id;
		
			$db=Application_Model_dataDAO_DBConnect::conn();
			$value=$db->update('tb_danhmucsanpham',$update,$conditions);
			if($value==1) $result=true;
			$db->closeConnection();
		} catch (Zend_Db_Exception  $e) {
			echo $e->getMessage();
		}
		
		return $result;
	}

	
	
}

?>